from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_admin.entity.do.kq_log_data_do import KhLogData
from module_admin.entity.vo.kq_log_data_vo import Kq_log_dataModel, Kq_log_dataPageQueryModel
from utils.page_util import PageUtil


class Kq_log_dataDao:
    """
    考勤原始数据模块数据库操作层
    """

    @classmethod
    async def get_kq_log_data_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据序号获取考勤原始数据详细信息

        :param db: orm对象
        :param id: 序号
        :return: 考勤原始数据信息对象
        """
        kq_log_data_info = (
            (
                await db.execute(
                    select(KhLogData)
                    .where(
                        KhLogData.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return kq_log_data_info

    @classmethod
    async def get_kq_log_data_detail_by_info(cls, db: AsyncSession, kq_log_data: Kq_log_dataModel):
        """
        根据考勤原始数据参数获取考勤原始数据信息

        :param db: orm对象
        :param kq_log_data: 考勤原始数据参数对象
        :return: 考勤原始数据信息对象
        """
        kq_log_data_info = (
            (
                await db.execute(
                    select(KhLogData).where(
                        KhLogData.user_id == kq_log_data.user_id if kq_log_data.user_id else True,
                        KhLogData.user_name == kq_log_data.user_name if kq_log_data.user_name else True,
                        KhLogData.am_pm == kq_log_data.am_pm if kq_log_data.am_pm else True,
                        KhLogData.kh_work == kq_log_data.kh_work if kq_log_data.kh_work else True,
                        KhLogData.punch_date == kq_log_data.punch_date if kq_log_data.punch_date else True,
                        KhLogData.punch_time == kq_log_data.punch_time if kq_log_data.punch_time else True,
                        KhLogData.punch_result == kq_log_data.punch_result if kq_log_data.punch_result else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return kq_log_data_info

    @classmethod
    async def get_kq_log_data_list(cls, db: AsyncSession, query_object: Kq_log_dataPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取考勤原始数据列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 考勤原始数据列表信息对象
        """
        query = (
            select(KhLogData)
            .where(
                KhLogData.user_id == query_object.user_id if query_object.user_id else True,
                KhLogData.user_name.like(f'%{query_object.user_name}%') if query_object.user_name else True,
                KhLogData.am_pm == query_object.am_pm if query_object.am_pm else True,
                KhLogData.kh_work == query_object.kh_work if query_object.kh_work else True,
                KhLogData.punch_date == query_object.punch_date if query_object.punch_date else True,
                KhLogData.punch_time == query_object.punch_time if query_object.punch_time else True,
                KhLogData.punch_result == query_object.punch_result if query_object.punch_result else True,
            )
            .order_by(KhLogData.id)
            .distinct()
        )
        kq_log_data_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return kq_log_data_list

    @classmethod
    async def add_kq_log_data_dao(cls, db: AsyncSession, kq_log_data: Kq_log_dataModel):
        """
        新增考勤原始数据数据库操作

        :param db: orm对象
        :param kq_log_data: 考勤原始数据对象
        :return:
        """
        db_kq_log_data = KhLogData(**kq_log_data.model_dump(exclude={}))
        db.add(db_kq_log_data)
        await db.flush()

        return db_kq_log_data

    @classmethod
    async def edit_kq_log_data_dao(cls, db: AsyncSession, kq_log_data: dict):
        """
        编辑考勤原始数据数据库操作

        :param db: orm对象
        :param kq_log_data: 需要更新的考勤原始数据字典
        :return:
        """
        await db.execute(update(KhLogData), [kq_log_data])

    @classmethod
    async def delete_kq_log_data_dao(cls, db: AsyncSession, kq_log_data: Kq_log_dataModel):
        """
        删除考勤原始数据数据库操作

        :param db: orm对象
        :param kq_log_data: 考勤原始数据对象
        :return:
        """
        await db.execute(delete(KhLogData).where(KhLogData.id.in_([kq_log_data.id])))

